<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-left">
        <img src="../assets/heima.png" alt="">
        <div class="title">电商后台管理系统</div>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="asideWidth">
        <div class="top-bar" @click="isCollapse = !isCollapse">|||</div>
        <!-- 菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="(item, i) in menuList" :key="item.id">
            <template slot="title">
              <i :class="['iconfont', icons[i]]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + item2.path" v-for="item2 in item.children" :key="item2.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      icons: [
        'icon-users',
        'icon-tijikongjian',
        'icon-shangpin',
        'icon-danju',
        'icon-baobiao'
      ],
      isCollapse: false
    }
  },
  computed: {
    // 当前被激活的左侧菜单项
    activePath() {
      return this.$route.path
    },
    // 侧边栏的动态宽度
    asideWidth() {
      return this.isCollapse ? '64px' : '200px'
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    // 退出登录
    async logout() {
      // 询问用户是否退出登录
      const confirmResult = await this.$confirm('您确认退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 取消退出登录的操作
      if (confirmResult !== 'confirm') return

      // 确认退出登录
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header-left {
    display: flex;
    align-items: center;
    img {
      margin-right: 15px;
    }
    .title {
      font-size: 22px;
      color: white;
    }
  }
}
.el-aside {
  background-color: #333744;
  user-select: none;
}
.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.el-menu-item {
  font-size: 12px;
}

/deep/ .el-submenu__title {
  font-size: 12px;
}

.el-menu {
  border-right: 0;
}

.top-bar {
  background-color: #4a5064;
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 22px;
  cursor: pointer;
  letter-spacing: 0.12em;
}
</style>